//
//  LDEditMyResumeTextResumeVC.swift
//  DingDongZP
//
//  Created by 三秒文化 on 16/4/18.
//  Copyright © 2016年 iyhjiang.com. All rights reserved.
//

import UIKit
import ReactiveCocoa

class LDEditMyResumeTextResumeVC: LDBaseCustomizeVC {

    var myResumeModel:DDMyResumeModel?
//    var myResumeModel:LDMyResumeModel?
    var workExpArr = [LDMyResumeWorkexperienceModel]()
    var eduExpArr = [LDMyResumeEduexperienceModel]()
    
    var titleArr = ["我的亮点:","我的优势:"]
    var descTextArr = [String]()
    
    var bottomBtn:UIButton?
    
    //显示类型
    var type:LDEditMyResumeVCType?
    
    // 亮点标签
    var highlightsIDParams = [String]()
    var cellDict = [Dictionary<String,AnyObject>]()
    
//    var heightLightCell:LDBaseTitleDescCell?
    var expOrEduCell:LDMyResumeBaseCellInAll_Edit?
//    var eduCell:LDMyResumeBaseCellInAll_Edit?
    //
    var cellH:CGFloat = 0
    
    var tableView:LDBaseTableView!
    
    override func viewDidLoad() {
        
        super.viewDidLoad()
        initUI()
        
        
        if type == .HeigthLightStrength {
            
            
            titleArr = ["我的亮点:","我的优势:"]
            
//            let cell = LDBaseTitleDescCell.init(style: .Default, reuseIdentifier: NSStringFromClass(LDBaseTitleDescCell.classForCoder()))
//            heightLightCell = cell
            
        } else if type == .WorkExp{
            
            
            titleArr = ["工作经历:"]
            bottomBtn?.setTitle("添加工作经历", forState: .Normal)
            
            let cell = LDMyResumeCellExperienceInAll_Edit(style: .Default, reuseIdentifier: NSStringFromClass(LDMyResumeCellExperienceInAll_Edit.classForCoder()))
            
            cell.tapEditBtn({ [weak self] (index) in
                
                let vc = LDEditMyResumeAddEditJobExperience()
                
                guard let workExpDictList = LDUserInfo.sharedInstance.myRemuseInfoDict["workexperience"] as? [AnyObject] else { return }
                guard let workDic = workExpDictList[index] as? NSDictionary else { return }

                
                vc.type = .Edit
                vc.workModel = LDMyResumeWorkexperienceModel.yy_modelWithDictionary(workDic as [NSObject : AnyObject])
                self?.navigationController?.pushViewController(vc, animated: true)
                
            })
            
            expOrEduCell = cell
            
            
        } else if type == .EduExp {
            
            
            titleArr = ["教育经历:"]
            bottomBtn?.setTitle("添加教育经历", forState: .Normal)
            
            
            let cell = LDMyResumeCellEduInAll_Edit(style: .Default, reuseIdentifier: NSStringFromClass(LDMyResumeCellEduInAll_Edit.classForCoder()))
//            LDMyResumeEduexperienceModel
            
            cell.tapEditBtn({ [weak self] (index) in
                
                let vc = LDEditMyResumeAddOREditEdu()
                
                guard let workExpDictList = LDUserInfo.sharedInstance.myRemuseInfoDict["eduexperience"] as? [AnyObject] else { return }
                guard let workDic = workExpDictList[index] as? NSDictionary else { return }
            
                vc.type = .Edit
                vc.eduModel = LDMyResumeEduexperienceModel.yy_modelWithDictionary(workDic as [NSObject : AnyObject])
                self?.navigationController?.pushViewController(vc, animated: true)
                
            })
            
            expOrEduCell = cell
        }
        
        
        
       
        
        //
        if type == .WorkExp ||  type == .EduExp {
           
            view.addSubview(bottomBtn!)
            
            bottomBtn?.bk_whenTapped({ [weak self] in
                
                
                var vc:LDBaseCustomNavTabVC?
                if self?.type == .WorkExp {
                    
                    vc = LDEditMyResumeAddEditJobExperience()
                    self?.navigationController?.pushViewController(vc!, animated: true)
                    
                } else if self?.type == .EduExp {
                    
                    vc = LDEditMyResumeAddOREditEdu()
                    self?.navigationController?.pushViewController(vc!, animated: true)
                }
                
                
            })
            
            bottomBtn?.snp_makeConstraints(closure: { (make) in
                make.left.equalTo(LDMargin_H*4)
                make.right.equalTo(-LDMargin_H*4)
                make.height.equalTo(45)
                make.bottom.equalTo(-30)
            })
            
            tableView.snp_makeConstraints { (make) in
                
                make.top.left.right.equalTo(0)
                make.bottom.equalTo(bottomBtn!.snp_top)
            }
        } else {
            
            tableView.snp_makeConstraints { (make) in
                
                make.top.left.equalTo(0)
                make.bottom.right.equalTo(0)
            }
        }
        
    }
    
    func initUI() {
        
        view.frame = LDFrame.ScreenBounds
        
        tableView = LDBaseTableView()
        view.addSubview(tableView)
        
        tableView.contentInset = UIEdgeInsetsMake(55, 0, 10, 0)
        tableView.backgroundColor = LDGolbalColor.BgGoundGray
        tableView.separatorStyle = UITableViewCellSeparatorStyle.None
        tableView.delegate = self
        tableView.dataSource = self
        
        bottomBtn = UIButton.setDetailsBtn("添加", picName: nil, btnBackColor: LDGolbalColor.Green, titleColor: LDFontColor.White)
    }
    
    override func viewDidAppear(animated: Bool) {
        
        LDUserInfo.sharedInstance.updataJobFinderMyResume({ [weak self] (dict) in
            
            self?.descTextArr.removeAll()
            self?.updateDefaultData()
            
        }, failure: nil)
        
        LDUserInfo.sharedInstance.updateUserInfo({ [weak self] (dict) in
            
            self?.descTextArr.removeAll()
            self?.updateDefaultData()
        }, failure: nil)
        
    }
    
    //设置基本数据
    func updateDefaultData() {
        
        myResumeModel = DDMyResumeModel.yy_modelWithDictionary(LDUserInfo.sharedInstance.myRemuseInfoDict)
        highlightsIDParams = myResumeModel!.highlights.map({ return $0.tagid ?? ""})
        
        var tagText = ""
        
        for (index,item) in myResumeModel!.highlights.enumerate() {
            
            if index == 0 {
                
                tagText.appendContentsOf(item.tag)
            } else {
                tagText.appendContentsOf(" | " + item.tag)
            }
        }
        
        descTextArr.append(tagText)
        descTextArr.append(myResumeModel?.mystrengthstext ?? "" )
        
        cellH = setCellH()
        tableView.reloadData()
    }
    //
    func setCellH() ->CGFloat {
        
        if type == .HeigthLightStrength {
            
            return 60
        } else if (type == .WorkExp || type == .EduExp) {
            
            return expOrEduCell!.getCellHeight(myResumeModel!)
        }  else {
            
            return 0
        }
    }
}
extension LDEditMyResumeTextResumeVC:UITextViewDelegate,UITableViewDelegate,UITableViewDataSource {
    
    // MARK: - Table view data source
     func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        
        if myResumeModel == nil { return 0 }
        if type == LDEditMyResumeVCType.HeigthLightStrength {
            
            return 2
        } else if type == .WorkExp {
            
            return 1
        }  else if type == .EduExp {
            
            return 1
        } else {
            
            return 0
        }
    }
    
     func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
        return 1
    }
    
    
     func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    
        return creatCell(indexPath)
    }
    
    func creatCell(indexPath:NSIndexPath) -> UITableViewCell {
        
        let cell = creatDefaultCell(indexPath)
        return cell
    }
}

extension LDEditMyResumeTextResumeVC {
    
    //
    func creatDefaultCell(indexPath:NSIndexPath) ->UITableViewCell{
        
        if type == .HeigthLightStrength {
            
            var cell = tableView.dequeueReusableCellWithIdentifier(NSStringFromClass(LDBaseTitleDescCell.classForCoder())) as?LDBaseTitleDescCell
        
            if cell == nil {
                
                cell = LDBaseTitleDescCell(style: .Default, reuseIdentifier: NSStringFromClass(LDBaseTitleDescCell.classForCoder()))
                cell!.titleLab?.text = titleArr[indexPath.section]
                cell!.divideLine?.hidden = true
            }
            if myResumeModel != nil {
                cell!.descLab?.text = descTextArr[indexPath.section]
            }
            return cell!
            
        } else if type == .WorkExp || type == .EduExp {
            
            var cell = tableView.dequeueReusableCellWithIdentifier(NSStringFromClass(expOrEduCell!.classForCoder)) as? LDMyResumeBaseCellInAll_Edit
            if cell == nil {
                
                cell = expOrEduCell
            }
            
            cell?.model = myResumeModel
            return cell!
            
        } else {
            
            return UITableViewCell()
        }
    }
    
    //
     func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        
        return cellH
    }
    
     func tableView(tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
        let secV = UIView()
        secV.backgroundColor = LDGolbalColor.BgGoundGray
        return secV
    }
    
     func tableView(tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        
        if type == .HeigthLightStrength {
            
            return 10
        } else {
        
            return 0
        }
        
    }
    
    //
     func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

        
        
        if indexPath.section == 0 && type == .HeigthLightStrength {
            
            let vc = LDEditMyResumeHightLight()
            vc.title = "我的亮点"
            vc.preSelHightIDParams = highlightsIDParams
            navigationController?.pushViewController(vc, animated: true)
            
        } else if indexPath.section == 1 && type == .HeigthLightStrength  {
            
            let vc = LDEditMyResumeAdvantage()
            navigationController?.pushViewController(vc, animated: true)
            
        }
    }
}